package cn.antho.org.controller;

import cn.antho.basic.anno.LogAnno;
import cn.antho.basic.anno.PreAuthorize;
import cn.antho.basic.util.AjaxResult;
import cn.antho.basic.util.PageList;
import cn.antho.org.domain.Department;
import cn.antho.org.domain.Employee;
import cn.antho.org.query.DepartmentQuery;
import cn.antho.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
@LogAnno
@PreAuthorize
public class DepartmentController {

    @Autowired
    private IDepartmentService IDepartmentService;

    // 增 or 改
    @PutMapping
    @LogAnno(method = "addOrUpdate", methodDes = "增加或修改")
    public AjaxResult addOrUpdate (@RequestBody Department department) {
        if (null == department.getId()) {
            IDepartmentService.add(department);
        } else {
            IDepartmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    // 删
    @DeleteMapping("/{id}")
    @LogAnno(method = "delete", methodDes = "删除")
    @PreAuthorize(name = "根据id删除", sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        IDepartmentService.delete(id);
        return AjaxResult.me().success();
    }

    // 查所有
    @GetMapping
    @ApiOperation(value = "查询所有部门数据")
    @LogAnno(method = "findAll", methodDes = "查询全部")
    public List<Department> findAll() {
        return IDepartmentService.findAll();
    }

    // 查一个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @LogAnno(method = "findById", methodDes = "根据id查询单个")
    @PreAuthorize(name = "根据id查询部门",sn = "department:findById")
    public Department findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return IDepartmentService.findById(id);
    }

    // 分页查询
    @PostMapping
    @LogAnno(method = "queryByPage", methodDes = "分页查询")
    @PreAuthorize(name = "分页查询部门",sn = "department:queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return IDepartmentService.queryByPage(query);
    }

    @PatchMapping
    @LogAnno(method = "batchDel", methodDes = "批量删除")
    @PreAuthorize(name = "批量删除部门",sn = "department:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        IDepartmentService.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping("/tree")
    @PreAuthorize(name = "查询部门树",sn = "department:tree")
    public List<Department> deptTree() {
        return IDepartmentService.deptTree();
    }

    // 根据部门查询
    @GetMapping("/findByDeptId/{id}")
    @LogAnno(method = "findByDept", methodDes = "根据部门查询员工")
    public List<Employee> findByDept(@PathVariable("id") Long id) {
        return IDepartmentService.findByDept(id);
    }

}
